package com.mchsdk.paysdk.bean;

import com.mchsdk.open.MCApiFactory;
import com.mchsdk.paysdk.config.MCHConstant;
import com.mchsdk.paysdk.jni.MCHKeyTools;
import com.mchsdk.paysdk.utils.MCLog;
import com.mchsdk.paysdk.utils.TextUtils;

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;

public class SdkDomain {

	private static final String TAG = "SdkDomain";

	public static final String KEY_CHANNEL_ID = "paysdk_promoteid";
	public static final String KEY_CHANNEL_NAME = "paysdk_promotename";
	public static final String KEY_GAME_ID = "paysdk_gameid";
	public static final String KEY_GAME_NAME = "paysdk_gamename";
	public static final String KEY_GAME_APP_ID = "paysdk_gameappid";
	public static final String KEY_SIGNKEY = "paysdk_signkey";
	public static final String KEY_IP_ADDRESS = "paysdk_address";

	private static SdkDomain keyUtil;

	public static SdkDomain getInstance() {
		if (keyUtil == null) {
			keyUtil = new SdkDomain();
		}
		return keyUtil;
	}

	private String channelId;
	private String channelName;
	private String gameId;
	private String gameName;
	private String gameAppId;

	private SdkDomain() {
	}

	public void init(Context context) {
		MCHConstant.getInstance().setMCHKEY(getKey(context));
		MCHConstant.getInstance().setIpAddress(getIPAdderss(context));
		MCHConstant.getInstance().initUrlInfo();
		getGameConfigInfo(context);
	}

	private void getGameConfigInfo(Context context){
		channelId = "";
		channelName = "";
		gameId = "";
		gameName = "";
		gameAppId = "";
// 		channelId = getMetaValue(context, KEY_CHANNEL_ID);
//		channelName = getMetaValue(context, KEY_CHANNEL_NAME);
//		gameId = getMetaValue(context, KEY_GAME_ID);
//		gameName = getMetaValue(context, KEY_GAME_NAME);
//		gameAppId = getMetaValue(context, KEY_GAME_APP_ID);
	}

	public String getChannelId() {
		if(TextUtils.isEmpty(channelId)){
			channelId = MCApiFactory.getMCApi().getMap().get(KEY_CHANNEL_ID);
		}
		return channelId;
	}

	public String getChannelName() {
		if(TextUtils.isEmpty(channelName)){
			channelName = MCApiFactory.getMCApi().getMap().get(KEY_CHANNEL_NAME);
		}
		return channelName;
	}

	public String getGameAppId() {
		if(TextUtils.isEmpty(gameAppId)){
			gameAppId = MCApiFactory.getMCApi().getMap().get(KEY_GAME_APP_ID);
		}
		return gameAppId;
	}

	public String getGameId() {
		if(TextUtils.isEmpty(gameId)){
			gameId = MCApiFactory.getMCApi().getMap().get(KEY_GAME_ID);
		}
		return gameId;
	}

	public String getGameName() {
		if(TextUtils.isEmpty(gameName)){
			gameName = MCApiFactory.getMCApi().getMap().get(KEY_GAME_NAME);
		}
		return gameName;
	}

	/**
 	* cp传的参数里面获得
 	*/
	private String getKey(Context context) {
		String paysdk_signkey = getMetaValue(context, KEY_SIGNKEY);
		if(TextUtils.isEmpty(paysdk_signkey)){
			paysdk_signkey = MCApiFactory.getMCApi().getMap().get(KEY_SIGNKEY);
		}
		if(!TextUtils.isEmpty(paysdk_signkey)){
			return MCHKeyTools.getInstance().secToNor(paysdk_signkey);
		}
//		String paysdk_signkey_unity = MCApiFactory.getMCApi().getMap().get("paysdk_signkey_unity");
//		if(!TextUtils.isEmpty(paysdk_signkey_unity)){
//			return paysdk_signkey_unity;
//		}
		return "mengchuang";
	}

	private String getIPAdderss(Context context) {
		String paysdk_ipaddress = getMetaValue(context, KEY_IP_ADDRESS);
		if(TextUtils.isEmpty(paysdk_ipaddress)){
			paysdk_ipaddress = MCApiFactory.getMCApi().getMap().get(KEY_IP_ADDRESS);
		}
		return paysdk_ipaddress;
	}

	private String getMetaValue(Context context, String keystr){
		String res = "";
		try {
			res = getAppInfo(context).metaData.getString(keystr, "");
		}catch (Exception e){
			MCLog.w(TAG, keystr + " is null." + e.toString());
			res = "";
		}
		return  res;
	}

	private ApplicationInfo getAppInfo(Context context) {
		if (null == context) {
			return null;
		}
		ApplicationInfo appInfo;
		try {
			appInfo = context.getPackageManager().getApplicationInfo(
					context.getPackageName(), PackageManager.GET_META_DATA);
			return appInfo;
		} catch (PackageManager.NameNotFoundException e) {
			MCLog.w(TAG, "package name not found, error:" + e.toString());
			return null;
		}
	}

}
